#include "EbdModel.h"
#include "Common/Information.h"
#include "HandbrkUniteView.h"
#include "ssas_api.h"
#define LOG_TAG "HMI"
#include "log_api.h"

typedef enum 
{
    ALWAYS_OFF,
    ALWAYS_ON,
    ON_AND_POPUP,
} MachineState;

static MachineState currentStatus;
static Option_U8    showFlag;

static void init()
{
    currentStatus = ALWAYS_ON;
    showFlag.update_ = false;
    showFlag.value_ = 0;
}

static MachineState getNewState()
{
    MachineState newState = ALWAYS_ON;
    int msgState = COM_MSG_INIT;
    Std_ReturnType comRet = E_NOT_OK;
    do
    {
        if(!isFinishSelfTest(SELF_TEST_3S))
        {
            newState = ALWAYS_ON;
            break;
        }

        msgState = get_message_state(COM_IC_ANTILOCK_BRAKE_AND_TC_STATUS_HS);
        if(COM_MSG_INIT == msgState || COM_MSG_TIMEOUT == msgState)
        {
            newState = ALWAYS_ON;
            break;
        }

        uint16_t rawData = 0;
        comRet = Com_ReceiveSignal(COM_SID_BrkPdlDscrtInpSts, &rawData);
        if(E_OK != comRet)
        {
            newState = ALWAYS_ON;
            break;
        }

        if(OFF == rawData)
        {
            newState = ALWAYS_OFF;
        }
        else
        {
            if (isVehReady())
            {
                newState = ON_AND_POPUP;
            }
            else
            {
                newState = ALWAYS_ON;
            }
        }

    } while (false);
    

    return newState;
}

static void handlStateTransition(MachineState newStatus)
{
    if(newStatus != currentStatus)
    {
        switch (currentStatus)
        {
            case ON_AND_POPUP:
                //close popup todo
                break;

            case ALWAYS_OFF:
            case ALWAYS_ON:
                //ignore
                break;
            
            default:
                LOG_E("leave:ABS unknow state : %d\n", currentStatus);
                break;
        }

        currentStatus = newStatus;

        switch (currentStatus)
        {
            case ON_AND_POPUP:
                //show popup
                break;
            
            case ALWAYS_OFF:
            case ALWAYS_ON:
                //ignore
                break;

            default:
                LOG_E("entry:ABS unknow state : %d\n", currentStatus);
                break;
        }
    }
}

static void postprocess()
{
    switch (currentStatus)
    {
        case ON_AND_POPUP:
        {
            setOptionValue_U8(&showFlag, true);
            break;
        }

        case ALWAYS_ON:
        {
            setOptionValue_U8(&showFlag, true);
            break;
        }

        case ALWAYS_OFF:
        {
            setOptionValue_U8(&showFlag, false);
            break;
        }
        
        default:
            LOG_E("postprocess:ABS unknow state : %d\n", currentStatus);
            break;
    }
}

static void run()
{
    if(handbrk_unite_visble_mask)
    {
        MachineState newStatus = getNewState();
        handlStateTransition(newStatus);
        postprocess();
    }
}

static void release()
{

}

EbdModel g_EbdModelInstance = {
    {
        init,
        run,
        release,
        MIN_PERIOD,
    },
};

Option_U8 getEbdShowFlag()
{
    return showFlag;
}
